package com.qf.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * redis分布式锁工具类
 * @version v1.0.0
 * @Description:
 * @author: 姜红凯
 * @date: 2022.03.24
 * @since 1.0
 */
@Component
public class RedisLockUtil {
    @Autowired
    StringRedisTemplate redisTemplate;

    /**
     * 加锁
     * @param key
     * @param value
     * @param second
     * @return
     */
    public boolean lock(String key,String value,int second){
       return redisTemplate.opsForValue().setIfAbsent(key,value,second, TimeUnit.SECONDS);
    }

    /**
     * 解锁
     * @param key
     */
    public void unlock(String key){
        redisTemplate.delete(key);
    }

}
